package com.yonyou.crm.bpub.approval.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import yonyou.bpm.rest.utils.BaseUtils;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.yonyou.crm.common.page.entity.Page;

@Service
public class JsonResultService {
	private Logger log = LoggerFactory.getLogger(getClass());

    private ObjectMapper mapper = new ObjectMapper();

    public JsonResultService() {
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public String toJson(Object obj) {
        try {
            String json = mapper.writeValueAsString(obj);
            return json;
        } catch (JsonProcessingException e) {
            log.error("序列化对象为json时异常", e);
            return e.getMessage();
        }
    }

    public <T> T toObject(String json, Class<T> cls) {
        try {
            //log.debug("反序列化对象.json=[" + json + "],cls=" + cls.getName());
            return mapper.readValue(json, cls);
        } catch (Exception e) {
            log.error("反序列化对象失败.json=[" + json + "],cls=" + cls.getName(), e);
            return null;
        }
    }
//
//    public ArrayNode createArrayNode() {
//        return mapper.createArrayNode();
//    }
//
//    public ObjectNode createObjectNode() {
//        return mapper.createObjectNode();
//    }
    
    @SuppressWarnings("unchecked")
	public Map<String, Object> jsonNode2Map(JsonNode jsonNode) {
    	String json = toJson(jsonNode);
    	return (Map<String, Object>)toObject(json, Map.class);
    }
    
    /*
     * 按分页格式返回数据
     */
    @SuppressWarnings("unchecked")
	public Page<Map<String, Object>> jsonNode2Page(JsonNode jsonNode, int page, int pageSize) {
    	Page<Map<String, Object>> pageData = new Page<Map<String,Object>>();
    	Map<String, Object> map = jsonNode2Map(jsonNode);
    	
    	pageData.setTotal((Integer)map.get("total"));
    	pageData.setPage(page);
    	pageData.setPageSize(pageSize);
    	pageData.setData((List<Map<String, Object>>)map.get("data"));
    	return pageData;
    }
    
    /*
     * 按分页格式返回数据
     */
	public <T> Page<T> jsonNode2Page(JsonNode jsonNode, int page, int pageSize, Class<T> cls) {
    	Page<T> pageData = new Page<T>();
    	
    	pageData.setTotal(jsonNode.get("total").asInt());
    	pageData.setPage(page);
    	pageData.setPageSize(pageSize);
    	
    	List<T> list = jsonNode2List(jsonNode, cls);
    	
    	pageData.setData(list);
    	return pageData;
    }
	
	public <T> List<T> jsonNode2List(JsonNode jsonNode, Class<T> cls) {
		ArrayNode arrayNode = BaseUtils.getData(jsonNode);
		List<T> list = new ArrayList<T>();
    	for (int i = 0; arrayNode != null && i < arrayNode.size(); i++) {
            T resp = toObject(arrayNode.get(i).toString(), cls);
            list.add(resp);
        }
    	return list;
	}
}
